package cn.edu.sanxiau.www.controller;


import java.io.IOException;
import java.util.HashMap;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import cn.edu.sanxiau.www.model.Permission;
import cn.edu.sanxiau.www.model.User;
import cn.edu.sanxiau.www.servier.UserServier;
import cn.edu.sanxiau.www.servier.impl.UserServierImpl;
import frame.controller.BaseController2;

@WebServlet(urlPatterns = "/loginController.do")
public class LoginController extends BaseController2 {

    /**
     *
     */
    private static final long serialVersionUID = 5458427071620050141L;

    public String login(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1.接收数据；
        String uaername = req.getParameter("uaername");
        String password = req.getParameter("password");

        //封裝
        User user = new User();
        user.setUaername(uaername);
        user.setPassword(password);

        //3.调用业务；
        User user_db = userServier.queryUserByUser(user);


        //优化程序：非空判断
        if (user_db != null) {
            if (user_db.getUaername().equals(uaername) && user_db.getPassword().equals(password)) {
                //把登录用户信息保存到Session
                HttpSession session = req.getSession();
                session.setAttribute("USER", user_db);
                //################################# 权限  ##############################
                // 1、登录用户信息ID,查询拥有的菜单
                int user_id = user_db.getUser_id();
                List<Permission> firstLvMenuList = permissionService.queryUser_xz_permissionByUser_id(user_id);

                // 2、实例化HashMap
                HashMap<Integer, List<Permission>> menuLvMap = new HashMap<>();
                for (Permission permission : firstLvMenuList) {
//						System.out.println("------------数据库返回对象----------"+permission);	
                    int p_id = permission.getPermission_id();
                    List<Permission> permissionSelectedListSon = permissionService.queryXZPermissionAllSonByP_id(p_id);
//				            for (Permission permissionSon : permissionSelectedListSon) {
//				            	System.out.println("______permissionSon______" + permissionSon);
//							}	
                    // 维护父节点和子节点关系
                    menuLvMap.put(p_id, permissionSelectedListSon);
                }
                req.setAttribute("firstLvMenuList", firstLvMenuList);
                req.setAttribute("menuLvMap", menuLvMap);

                //################################# 权限  ##############################
                //友情提示
                req.setAttribute("message", "登录成功！");
                //2.跳转页面；
                return "view/frame/main.jsp";
            } else {
                //友情提示
                req.setAttribute("message", "用户名或密码错误，请重新输入！");
                //2.跳转页面；
                return "login.jsp";
            }

        } else {
            return "login.jsp";
        }

    }

}
